package logic

import (
	"context"
	"fmt"
	"shopping-mall/models"

	"shopping-mall/shopping_rpc/internal/svc"
	"shopping-mall/shopping_rpc/shopping_rpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type RateProductLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewRateProductLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RateProductLogic {
	return &RateProductLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *RateProductLogic) RateProduct(in *shopping_rpc.RateProductRequest) (*shopping_rpc.RateProductResponse, error) {
	// todo: add your logic here and delete this line
	rate := models.Rate{
		UserId:    int(in.UserId),
		ProductId: int(in.ProductId),
		Content:   in.Content,
	}
	if models.DB.Create(&rate).Error != nil {
		return nil, fmt.Errorf("服务器繁忙")
	}
	return &shopping_rpc.RateProductResponse{}, nil
}
